package org.myself.meeting.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

// @Configuration注解该类，等价于XML中配置beans标签，
// @Bean相当于spring xml里面的bean标签，
// @EnableWebSocket这个注解用于springmvc对websocket的支持。

@Configuration
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
// WebSocketPushHandler是websocket消息处理类，包括窗口打开，关闭，信息发送，错误信息监听等功能。
// MyWebSocketInterceptor是登录信息拦截处理的类
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //登录拦截信息处理类
        registry.addHandler(WebSocketPushHandler(),"/webSocketServer").addInterceptors(new MyWebSocketInterceptor());
        //低版本的访问
        registry.addHandler(WebSocketPushHandler(), "/sockjs/webSocketServer").addInterceptors(new MyWebSocketInterceptor())
                .withSockJS();
    }
    @Bean
    public WebSocketHandler WebSocketPushHandler(){
        //消息处理类
        return new WebSocketPushHandler();
    }
}
